<template>
    <iframe id="w7iframe" :src="url" v-loading="loading" frameborder="0" @onload="loading=false" @error="loading=false" class="iframe"></iframe>
</template>

<script>
export default {
    data(){
        return {
            loading: true,
            url: "",
        }
    },
    props: ['microData','linkurl','baseurl','domain'],
    created(){
        this.changeRoute();
        this.listener();
    },
    methods: {
        listener(){
            window.addEventListener('message', (e) => {
                let msg = e.data;
                this.$nextTick(()=>{
                    switch(msg?.type){
                        case 'paymet': this.payment(msg.data); break;
                        case 'pay': this.pay(msg.ticket); break;
                        case 'get_storage_key': this.get_storage_key(); break;
                        case 'login': this.login(); break;
                        case 'changeMenuCollapse': this.changeMenuCollapse(msg.value); break;
                        case 'jsTicket': this.jsTicket(); break;
                        case 'realname': this.realname(); break;
                        case 'getModuleInfo': this.getModuleInfo(); break;
                        case 'inviteUser': this.inviteUser(); break;
                        case 'appcont': this.appcontOpen(msg.data); break;
                        case 'returnAppcont': this.returnAppcont(msg.data); break;
                    }
                })
            });
        },
        handleDataChange(e){
            const path = e.detail.data.path;
            if(path){
                this.$router.push(path);
            }
        },
        changeRoute(){
            if(this.domain){
                let domain = this.domain.replace(/\/$/,'');
                this.url = domain + this.$route.fullPath.replace(this.baseurl,'');
                return;
            }
            this.url = this.linkurl.replace(/\/$/,'') + this.$route.fullPath.replace(this.baseurl,'');
        },
        payment(){this.$message.warning("暂不支持");},
        pay(){this.$message.warning("暂不支持");},
        get_storage_key(){document.getElementById("w7iframe").contentWindow.postMessage({w7:true, type:"w7_get_storage_key", data:'storage_key'},'*');},
        login(){document.getElementById("w7iframe").contentWindow.postMessage({w7:true, type:"w7_login", data:{code:Date.now()}},'*');},
        changeMenuCollapse(boo){this.$store.commit("setMenuCollapse",boo);},
        jsTicket(){document.getElementById("w7iframe").contentWindow.postMessage({w7:true, type:"w7_jsTicket", data:{js_ticket: 123456}},'*');},
        realname(){this.$message.warning("暂不支持");},
        getModuleInfo(){
            document.getElementById("w7iframe").contentWindow.postMessage({w7:true, type:"w7_getModuleInfo", data: {
                module_name: 'module_name',
                module_version: 'module_version'
            }},'*');
        },
        inviteUser(){this.$message.warning("暂不支持");},
        appcont(){this.$message.warning("暂不支持");},
        returnAppcont(){this.$message.warning("暂不支持");},
    },
    watch:{
        $route(){
            this.changeRoute();
        }
    }
}
</script>

<style scoped>
.iframe{display:block; width:100%; min-height:100%;}
</style>